import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import expedia.HotelBrand;
import expedia.HotelChain;
import expedia.HotelInfoResponse;
import expedia.HotelInformationResponseExpedia;
import expedia.chain.ChainInfoResponse;
import expedia.region.RegionInfo;
import expedia.region.RegionInfoResponse;
import org.apache.commons.lang3.StringUtils;
import service.ExpediaHttpService;
import service.ExpediaHttpServiceImpl;

import java.io.*;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.logging.SimpleFormatter;

/**
 * @description: 测试
 * @author: yuanjianpeng5
 * @createdate: 2020-01-03 12:38
 * @lastdate:
 */

public class Test {

    public static void main(String[] args) throws IOException {
        long startTime = System.currentTimeMillis();
        System.out.println("开始请求数据" + String.valueOf(startTime));
        String op = "chain", hotelId = "";
        ExpediaHttpService expediaHttpService = new ExpediaHttpServiceImpl();
        String path = new SimpleDateFormat("yyyy_MM_dd_hh_mm_ss").format(new Date());
        File f = new File("D:/" + op + "_" + path + ".txt");
        OutputStream out = new FileOutputStream(f);
        OutputStreamWriter writer = new OutputStreamWriter(out, "UTF-8");
        boolean  hasNextPage = true;
        String netPageUrl = null;
        int count = 0;
        switch (op) {
            case "content":
                while (hasNextPage) {
                    HotelInfoResponse result = expediaHttpService.getHotelInfoCN(hotelId, null, netPageUrl);
                    if (null != result && StringUtils.isEmpty(result.getType())) { //正常取得返回值时作处理
                        Map<String, HotelInformationResponseExpedia> resultRegionInfo = result.getHotelInformationResponse();
                        for (Map.Entry<String, HotelInformationResponseExpedia> entry : resultRegionInfo.entrySet()) {
                            HotelInformationResponseExpedia value = JSONObject.parseObject(JSON.toJSONString(entry.getValue()), HotelInformationResponseExpedia.class);
                            writer.append(value.getHotelId() + ":" + value.getName() + value.getDescriptions().getHeadline() + "\r\n"); //+ "-" + value.getDescriptions().getHeadline()
                        }
                        if (!StringUtils.isEmpty(result.getLink())) {
                            hasNextPage = true;
                            netPageUrl = result.getLink();
                        } else {
                            hasNextPage = false;
                        }
                    } else {
                        hasNextPage = false;
                        writer.append(result.getMessage() + "\r\n");
                    }
                    count ++;
                }
                break;
            case "region":
                while (hasNextPage) {
                    RegionInfoResponse result = expediaHttpService.getHotelOfRegion(netPageUrl, "zh-CN", "property_ids", null);
//                    String path = new SimpleDateFormat("yyyy_MM_dd_hh_mm_ss").format(new Date());
//                    File f = new File("D:/" + op + "_" + path + ".txt");
//                    OutputStream out = new FileOutputStream(f);
//                    OutputStreamWriter writer = new OutputStreamWriter(out, "UTF-8");
                    if (null != result && StringUtils.isEmpty(result.getType())) { //正常取得返回值时作处理
                        Map<String, RegionInfo> resultRegionInfo = result.getRegionInfo();
                        for (Map.Entry<String, RegionInfo> entry : resultRegionInfo.entrySet()) {
                            RegionInfo value = JSONObject.parseObject(JSON.toJSONString(entry.getValue()), RegionInfo.class);
                            List<String> hotelIds = value.getPropertyIds();
                            if (!hotelIds.isEmpty()) {
                                hotelIds.stream().forEach(e -> {
                                    try {
                                        writer.append(entry.getKey() + ":" +e + "\r\n");
                                    } catch (IOException ex) {
                                        ex.printStackTrace();
                                    }
                                });
                            }
                        }
                        if (!StringUtils.isEmpty(result.getLink())) {
                            hasNextPage = true;
                            netPageUrl = result.getLink();
                        } else {
                            System.out.println("正常结束");
                            hasNextPage = false;
                        }
                    } else {
                        System.out.println("发生异常, url=" + netPageUrl);
                        hasNextPage = false;
                        writer.append(result.getMessage() + "\r\n");
                    }
                    count++;
//                    writer.close();
//                    out.close();
                }
                break;
            case "chain":
                int chainCount = 0, brandCount = 0;
                ChainInfoResponse chainInfoResponse = expediaHttpService.getChain(null);
                if (!StringUtils.isEmpty(chainInfoResponse.getType())) {
                    System.out.println("数据请求发生异常, 异常信息=" + chainInfoResponse.getMessage());
                    return;
                }
                if (chainInfoResponse.getChainInfo().size() > 0) {
                    for (Map.Entry<String, HotelChain> entry : chainInfoResponse.getChainInfo().entrySet()) {
                        try {
                            HotelChain hotelChain = JSONObject.parseObject(JSON.toJSONString(entry.getValue()), HotelChain.class);
                            writer.append(hotelChain.getId() + ":" + hotelChain.getName() + "\r\n");
                            if (null != hotelChain.getBrands() && hotelChain.getBrands().size() > 0) {
                                for (Map.Entry<String, HotelBrand> entrys : hotelChain.getBrands().entrySet()) {
                                    HotelBrand hotelBrand = JSONObject.parseObject(JSON.toJSONString(entrys.getValue()), HotelBrand.class);
                                    writer.append("\t" + hotelBrand.getId() + ":" + hotelBrand.getName() + "\r\n");
                                    brandCount ++;
                                }
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                            System.out.println(JSON.toJSONString(entry.getValue()));
                        }
                        chainCount ++;
                    }
                }
                System.out.println("连锁数=" + chainCount);
                System.out.println("品牌数=" + brandCount);
                break;
            default:
                ;
        }
        writer.close();
        out.close();
        long endtTime = System.currentTimeMillis();
        System.out.println("总页数" + count);
        System.out.println("请求数据完成， 耗时" + String.valueOf(endtTime - startTime));
    }



    public static void mains(String[] args) {
//        String str = "<abdfv>".replaceAll("(<[^>]*>)", "");
        String str = "<https://api.ean.com/2.3/regions?token=DXF1ZXJ5QW5kRmV0Y2gBAAAAAAdcoBgWbUpHYTdsdFVRc2U4c0xfLUhGMzM1QQ>; rel=\"next\"; expires=\"2019-03-05T07:23:14.000Z\"";
        System.out.println(str.substring(1, str.indexOf(";")-1));
    }
}